
import {scanContent} from '@/api/dynamic.js'

export const statisticsFeedCount = {
    timein(_this,css){
		if(!css){
			css= ".page-content"
		}
		 
		
		let listInScreenContentIds=[];
	 
		return setInterval(()=>{					
					_this.$nextTick( async  () => {
							
						await new Promise((resolve, reject)=>{
							const query = uni.createSelectorQuery().in(_this);
							let windowHeight= uni.getSystemInfoSync().windowHeight;
							let windowWidth= uni.getSystemInfoSync().windowWidth;
							let selectcss = css;
							//#ifdef MP-BAIDU
							selectcss += ""
							//#endif
							//#ifndef MP-BAIDU
							selectcss += " >>> "
							//#endif
							selectcss +=' .statistics-dynamic-content';
							// console.log(selectcss,'selectcss')
							let queryselect =query.selectAll(selectcss);
							queryselect.boundingClientRect(data => {
								// console.log(data.length)
								if(data&&data.length>0){
									for (var i = 0; i < data.length; i++) {
										if(data[i].dataset.contentid){
											 
											let index = data[i].dataset.index; 
											//显示逻辑
											if(data[i].top>windowHeight){
												//顶部离开屏幕在下方
												//从列表里面删除 
												
												for (var j = 0; j < listInScreenContentIds.length; j++) {
													if(listInScreenContentIds[j]==data[i].dataset.contentid+"_"+index){
														listInScreenContentIds.splice(j,1) ;
														// console.log("delete",data[i].dataset.contentid)
													}
													
												}
											}else if (data[i].top+data[i].height<0){ 
												//底部离开屏幕在上方 
												//从列表里面删除
												for (var j = 0; j < listInScreenContentIds.length; j++) {
													if(listInScreenContentIds[j]==data[i].dataset.contentid+"_"+index){
														listInScreenContentIds.splice(j,1) 
														// console.log("delete",data[i].dataset.contentid)
													}
												}
												
											}else{//出现在屏幕内 
												
												//卡片高度的1/2以上出现在屏幕内才计算 或者占据屏幕高度1/2
												
												//显示高度  
												let inScreenHeight = 0;
												
												if(data[i].top>=0&&data[i].top<windowHeight){
													if(data[i].top+data[i].height>windowHeight){
														// 1.从底部露出
														inScreenHeight=windowHeight-data[i].top;
													}else{
														// 2.完全显示在屏幕
														inScreenHeight=data[i].height;
													}
												}
												
												if(data[i].top<0&&data[i].top+data[i].height>0){
													if(data[i].top+data[i].height<windowHeight){
														//3.顶部露出部分
														inScreenHeight=data[i].top+data[i].height;
													}else{
														//4.超过屏幕显示
														inScreenHeight = windowHeight;
													}
												}
												
												
												
												
												
												
												if(inScreenHeight>=data[i].height/2||inScreenHeight>=windowHeight/2){
													//进入屏幕判断是否已经在列表里面
													//判断是否已经统计到服务端
													//若已经统计则不统计 
													//若没有统计则统计然后记录统计
													let isInList=false ; 
													for (var j = 0; j < listInScreenContentIds.length; j++) {
														if(listInScreenContentIds[j]==data[i].dataset.contentid+"_"+index){
															isInList=true;
															break;
														}
													}
													if(!isInList){ 
														listInScreenContentIds.push(data[i].dataset.contentid+"_"+index);
														//todo 统计请求
														scanContent({contentId:data[i].dataset.contentid})
														
														
													}
												}
												
												 
												
												
												 
											} 
											
											
											
											
										}
									}							
								}
							}).exec()
						})
					})
					
					// console.log(listInScreenContentIds)
					
				},500)
	}
}